import 'dart:io';

import 'package:dmz/base/widget/defult_widget.dart';
import 'package:dmz/base/widget/defult_text_field.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'dart:ui' as ui;

import 'logo_widget.dart';

class LoginWidgets {
  static Widget logoWidget(context, {EdgeInsets? margin}) {
    return Container(
      margin: margin ??
          EdgeInsets.only(
              bottom: 96.sp, top: MediaQuery.of(context).padding.top + 70.sp),
      // width: 402.sp,
      // height: 368.sp,
      child: const LogotWidget(),
      // child: Image.asset('assets/images/login/logo.png', fit: BoxFit.cover),
    );
  }

  static Widget loginChanegBtns(bool isCodeLG, Function(bool) click) {
    return Row(mainAxisAlignment: MainAxisAlignment.center, children: [
      GestureDetector(
        onTap: () => click(true),
        child: DefaultText('验证码登陆'.tr, isCodeLG ? 48.sp : 36.sp,
            textColor:
                isCodeLG ? const Color(0xFFFFFFFF) : const Color(0xFFCECDCF)),
      ),
      SizedBox(width: 80.sp),
      GestureDetector(
        onTap: () => click(false),
        child: DefaultText('账号登录'.tr, !isCodeLG ? 48.sp : 36.sp,
            textColor:
                !isCodeLG ? const Color(0xFFFFFFFF) : const Color(0xFFCECDCF)),
      ),
    ]).paddingOnly(bottom: 48.sp);
  }

  static Widget inputWdiegts(bool isCodeLG,
      {TextEditingController? photneController,
      TextEditingController? passwordController,
      TextEditingController? codeController,
      String? countdownStr,
      String? btnTitle,
      bool? obscureText,
      int codeType = 0,
      Function()? netxt,
      Function()? sendCode,
      Function()? passwordClick}) {
    return Column(children: [
      inputWidget(
          hintText: '请输入手机号码'.tr,
          controller: photneController,
          otherWidet: cleanWidget(photneController),
          maxLength: 20),
      SizedBox(height: 32.sp),
      isCodeLG
          ? inputWidget(
              hintText: '请输入验证码'.tr,
              controller: codeController,
              maxLength: 6,
              otherWidet: countdownWidget(
                  codeType == 2
                      ? '重新发送'.tr
                      : codeType == 0
                          ? '发送验证码'.tr
                          : countdownStr ?? '',
                  sendCode))
          : inputWidget(
              hintText: '请输入密码'.tr,
              otherWidet: showPassWordWidget(passwordClick,
                  obscureText: obscureText ?? true),
              keyboardType: TextInputType.visiblePassword,
              obscureText: obscureText,
              controller: passwordController),
      loginBtn(netxt, title: btnTitle)
    ]);
  }

  ///设置密码手机验证码输入
  static Widget setPassWordWidget(TextEditingController photneController,
      TextEditingController codeController,
      {int codeType = 0, Function()? sendCode, String? countdownStr}) {
    return Column(mainAxisSize: MainAxisSize.min, children: [
      inputWidget(
          hintText: '请输入手机号码'.tr,
          controller: photneController,
          otherWidet: cleanWidget(photneController),
          maxLength: 20),
      SizedBox(height: 20.sp),
      inputWidget(
          hintText: '请输入验证码'.tr,
          controller: codeController,
          maxLength: 6,
          otherWidet: countdownWidget(
              codeType == 2
                  ? '重新发送'.tr
                  : codeType == 0
                      ? '发送验证码'.tr
                      : countdownStr ?? '',
              sendCode)),
      SizedBox(height: 20.sp),
    ]);
  }

  static Widget changePasswordInputWdiegts(
    bool isCodeLG, {
    TextEditingController? passwordController,
    TextEditingController? passwordControllerOther,
    String? btnTitle,
    Function()? finish,
  }) {
    return Column(children: [
      inputWidget(
          hintText: '请输入新的密码'.tr,
          controller: passwordController,
          otherWidet: cleanWidget(passwordController),
          maxLength: 20),
      SizedBox(height: 32.sp),
      inputWidget(
          hintText: '请再次输入新的密码'.tr,
          controller: passwordControllerOther,
          otherWidet: cleanWidget(passwordControllerOther),
          maxLength: 20),
      loginBtn(finish, title: btnTitle)
    ]);
  }

  static Widget inputWidget(
      {String? hintText,
      Function(String)? onChanged,
      Widget? otherWidet,
      int? maxLength,
      TextEditingController? controller,
      TextInputType? keyboardType,
      bool? obscureText}) {
    return Container(
        decoration: BoxDecoration(
          color: const Color(0xFFF5F5F5).withOpacity(0.2),
          borderRadius: BorderRadius.circular(120.0),
        ),
        height: 120.sp,
        margin: EdgeInsets.only(left: 40.sp, right: 40.sp),
        padding: EdgeInsets.only(left: 40.sp, right: 40.sp),
        child: Row(children: [
          Expanded(
              child: DefultTextField(
            42.sp,
            hintText: hintText,
            onChanged: onChanged,
            maxLength: maxLength,
            obscureText: obscureText,
            keyboardType: keyboardType ?? TextInputType.number,
            controlle: controller,
          )),
          otherWidet ?? const SizedBox()
        ]));
  }

  static Widget cleanWidget(TextEditingController? photneController) {
    return GestureDetector(
        onTap: () => photneController?.text = '',
        child: SizedBox(
            width: 48.sp,
            height: 48.sp,
            child: Image.asset('assets/images/login/input_clean.png')));
  }

  static Widget countdownWidget(String countdownStr, Function()? snedCode) {
    return GestureDetector(
        onTap: snedCode,
        child: Container(
            child: DefaultText(
          countdownStr,
          32.sp,
          textColor: const Color(0xFFA8FF00),
        )));
  }

  static Widget showPassWordWidget(Function()? click,
      {bool obscureText = false}) {
    return GestureDetector(
        onTap: click,
        child: SizedBox(
            width: 48.sp,
            height: 48.sp,
            child: Image.asset(
                'assets/images/login/${obscureText ? 'pub_hide' : 'pub_show'}.png')));
  }

  static Widget loginBtn(Function()? click,
      {String? title,
      Color? textColor,
      double? hight,
      bool hollow = false,
      EdgeInsetsGeometry? margin}) {
    return GestureDetector(
        onTap: click,
        child: Container(
          alignment: Alignment.center,
          decoration: BoxDecoration(
            gradient: const LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [Color(0xFF6DD400), Color(0xFF20C468)]),
            borderRadius: BorderRadius.circular(120.0),
          ),
          height: hight ?? 120.sp,
          margin:
              margin ?? EdgeInsets.only(left: 40.sp, right: 40.sp, top: 88.sp),
          child: DefaultText(title ?? '注册/登录'.tr, 32.sp,
              textColor: textColor ?? Colors.white,
              fontWeight: FontWeight.w600),
        ));
  }

  static Widget loginOtherBtn(BuildContext context, Function()? click,
      {String? title,
      Color? textColor,
      bool hollow = false,
      double? width,
      double? hight,
      double? textSize}) {
    return GestureDetector(
        onTap: click,
        child: Stack(alignment: Alignment.center, children: [
          Container(
              width: width ?? MediaQuery.of(context).size.width - 80.sp,
              height: hight ?? 120.sp,
              child: CustomPaint(
                painter: GradientCirclePainter(),
              )),
          DefaultText(title ?? '注册/登录'.tr, textSize ?? 32.sp,
              textColor: textColor ?? const Color(0xFF23C565),
              fontWeight: FontWeight.w600)
        ]));
  }

  static Widget otherActionWidgets(bool agreed, bool isCodeLG, Function() click,
      Function(int type) textClick,
      {Function()? forgetClick}) {
    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
      if (!isCodeLG)
        GestureDetector(
            onTap: forgetClick,
            child: DefaultText('${'忘记密码'.tr}？', 36.sp,
                    textColor: Colors.white, fontWeight: FontWeight.w600)
                .marginOnly(bottom: 32.sp)),
      Row(crossAxisAlignment: CrossAxisAlignment.start, children: [
        GestureDetector(
            onTap: click,
            child: Container(
                    width: 48.sp,
                    height: 48.sp,
                    child: Image.asset(
                        'assets/images/login/${agreed ? 'agreed_icon.png' : 'agreed_not_icon.png'}'))
                .marginOnly(right: 18.sp, top: 5.sp)),
        Expanded(
            child: getRichTexts('我已阅读并同意'.tr, '《用户协议》'.tr, '《隐私政策》'.tr,
                '首次自动注册新账号。'.tr, textClick))
        // Expanded(
        //     child: DefaultText('我已阅读并同意《用户协议》《隐私政策》首次自动注册新账号。', 32.sp,
        //         textColor: Colors.white))
      ])
    ]).marginOnly(left: 48.sp, right: 88.sp, top: 96.sp);
  }

  static RichText getRichTexts(String text1, String text2, String text3,
      String text4, Function(int type) click,
      {Color? defulColor, Color? selectColor, double? fontSize}) {
    fontSize = fontSize ?? 32.sp;
    return RichText(
        text: TextSpan(children: [
      TextSpan(
          text: text1,
          style:
              TextStyle(color: defulColor ?? Colors.white, fontSize: fontSize)),
      TextSpan(
        text: text2,
        style: TextStyle(color: const Color(0xFF20C468), fontSize: fontSize),
        recognizer: TapGestureRecognizer()..onTap = () => click(0),
      ),
      TextSpan(
        text: text3,
        style: TextStyle(color: const Color(0xFF20C468), fontSize: fontSize),
        recognizer: TapGestureRecognizer()..onTap = () => click(1),
      ),
      TextSpan(
          text: text4,
          style:
              TextStyle(color: defulColor ?? Colors.white, fontSize: fontSize))
    ]));
  }

  static Widget otherLoginWidgt() {
    return Column(crossAxisAlignment: CrossAxisAlignment.center, children: [
      Row(mainAxisAlignment: MainAxisAlignment.center, children: [
        Container(
          width: 160.sp,
          height: 2.sp,
          decoration: BoxDecoration(
              gradient: LinearGradient(
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight,
                  colors: [
                const Color(0xFFEEEEEE).withOpacity(0),
                const Color(0xFFE5E5E5)
              ])),
        ),
        DefaultText('其他登录方式'.tr, 24.sp, textColor: Colors.white)
            .marginOnly(left: 10.sp, right: 10.sp),
        Container(
          width: 160.sp,
          height: 2.sp,
          decoration: BoxDecoration(
              gradient: LinearGradient(
                  begin: Alignment.centerRight,
                  end: Alignment.centerLeft,
                  colors: [
                const Color(0xFFEEEEEE).withOpacity(0),
                const Color(0xFFE5E5E5)
              ])),
        )
      ]),
      Row(mainAxisAlignment: MainAxisAlignment.center, children: [
        GestureDetector(
            child: Image.asset('assets/images/login/login_wixin.png',
                width: 88.sp, height: 88.sp)),
        if (Platform.isIOS) SizedBox(width: 104.sp),
        if (Platform.isIOS)
          GestureDetector(
              child: Image.asset('assets/images/login/login_apple.png',
                  width: 88.sp, height: 88.sp))
      ]).marginOnly(top: 24.sp)
    ]).marginOnly(top: 78.sp);
  }
}

///渐变色边框
class GradientCirclePainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..strokeWidth = 2.sp
      ..style = PaintingStyle.stroke
      ..shader = ui.Gradient.linear(
        Offset(0, 0), // 渐变起始位置（左上角）
        Offset(0, size.height), // 渐变结束位置（左下角）
        [Color(0xFF6DD400), Color(0xFF20C468)],
      );

    double cornerRadius = 120.sp;
    Rect rect = Rect.fromLTRB(0, 0, size.width, size.height);
    RRect roundedRect =
        RRect.fromRectAndRadius(rect, Radius.circular(cornerRadius));

    canvas.drawRRect(roundedRect, paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}
